import { isUrl } from '../utils/utils';

const menuData = [{
  name: 'dashboard',
  icon: 'dashboard',
  path: 'dashboard',
  // authority: 'admin',
  children: [{
    name: '控制台',
    path: 'Console',
  }],
}, {
  name: 'Chart',
  icon: 'area-chart',
  path: 'chart',
  children: [{
    name: '日报表',
    path: 'daychart',
  }, {
    name: '月报表',
    path: 'monthchart',
  }, {
    name: '年报表',
    path: 'yearchart',
  }],
}, {
  name: '系统设置',
  icon: 'setting',
  path: 'setting',
  authority: 'admin',
}];

// 遍历导航栏方法 data 为导航栏数组数据 parentPath 默认值为 / parentAuthority 为authority 权限
function formatter(data, parentPath = '/', parentAuthority) {
  return data.map((item) => {
    let { path } = item;
    if (!isUrl(path)) {
      path = parentPath + item.path;
    }
    // console.log(item);
    const result = {
      ...item,
      path,
      authority: item.authority || parentAuthority,
    };
    // console.log(parentAuthority);
    if (item.children) {
      result.children = formatter(item.children, `${parentPath}${item.path}/`, item.authority);
    }
    // console.log(result);
    return result;
  });
}

export const getMenuData = () => formatter(menuData);
